/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cqcis.uip.base.framework.service.impl;


import com.cqcis.uip.base.framework.FrameworkConstants;
import com.cqcis.uip.base.framework.UIPServiceException;
import com.cqcis.uip.base.framework.common.InterfaceLog;
import com.cqcis.uip.base.framework.common.Task;
import com.cqcis.uip.base.framework.service.InterfaceLogService;
import com.cqcis.uip.base.framework.service.StreamLogOutputService;
import com.cqcis.uip.base.framework.service.StreamNameUtil;
import com.cqcis.uip.base.framework.service.StreamOutput;
import com.cqcis.uip.base.framework.utils.ByteBuffer;
import com.cqcis.uip.base.framework.utils.StringUtil;
import com.cqcis.uip.base.framework.utils.TimeUtil;

/**
 * 实现
 * 
 * @author huangmy
 * @date   2008-11-11
 */
public class StreamLogOutputServiceImpl implements StreamLogOutputService {
	private StreamNameUtil streamNameUtil; // 生成文件名
    private StreamOutput   streamOutput;  // 写文件
    private InterfaceLogService interfaceLogService; // 日志
    private String interfaceId; // 接口ID
    private boolean streamRecord = true; // 是否记录二进制
    
    public String write(String recordId, ByteBuffer buffer) {
    	if (!streamRecord) {
    		return null;
    	}
    	return write(recordId, this.streamNameUtil.getSendName(), true, buffer);
    }
    
    public void write(String recordId, String fileName, ByteBuffer buffer) {
    	if (!streamRecord) {
    		return;
    	}
    	write(recordId, getfileName(fileName, true), true, buffer);
    }
    
	public String write(Task task, String fileName, boolean sendFile, ByteBuffer buffer) {
		if (!streamRecord) {
    		return null;
    	}
		return write(task.getTaskId(), getfileName(fileName, sendFile), sendFile, buffer);
	}
	
	public void write(String recordId, ByteBuffer sendBuffer, ByteBuffer recvBuffer) {
		if (!streamRecord) {
    		return;
    	}
		write(recordId, this.streamNameUtil.getSendName(), sendBuffer, 
				this.streamNameUtil.getRecvName(this.streamNameUtil.getPreFileSuffix()), recvBuffer);
	}
	
	public String write(String recordId, String fileName, boolean sendFile, ByteBuffer buffer) {
		if (!streamRecord) {
    		return null;
    	}
		
		fileName = getfileName(fileName, sendFile);
		
		String filepath = null;
		
		try {
			filepath = this.streamOutput.write(this.streamNameUtil.getFilePath(),
				fileName, buffer.buf());
		} catch (Exception e) {
			throw new UIPServiceException(e);
		}
		
		String file = getRecordPathFrom(filepath);
		String timeNow = TimeUtil.getCurrentTime();
		
		recordId = (recordId == null) ? "ignore" : recordId;
		
		InterfaceLog log = new InterfaceLog(recordId, this.interfaceId);
		if (sendFile) {
			log.setSendRecord(file);
			log.setSendTime(timeNow);
			log.setRecvRecord("ignore");
		} else {
			log.setRecvRecord(file);
			log.setRecvTime(timeNow);
			log.setSendRecord("ignore");
		}
		this.interfaceLogService.insert(log);
		log.setSaved(true);
		
		return filepath;
	}
	
	public void write(String recordId, String sendFileName,
			ByteBuffer sendBuffer, String recvFileName, ByteBuffer recvBuffer) {
		if (!streamRecord) {
    		return;
    	}
		
		sendFileName = getfileName(sendFileName, true);
		recvFileName = getfileName(recvFileName, false);
		
		String sendfilepath = null;
		
		try {
			sendfilepath = this.streamOutput.write(this.streamNameUtil
					.getFilePath(), sendFileName, sendBuffer.buf());
		} catch (Exception e) {
			throw new UIPServiceException(e);
		}
		String sendfile = getRecordPathFrom(sendfilepath);
		String timeNow = TimeUtil.getCurrentTime();
		
		String recvfilepath = null;
		try {
			recvfilepath = this.streamOutput.write(this.streamNameUtil
					.getFilePath(), recvFileName, recvBuffer.buf());
		} catch (Exception e) {
			throw new UIPServiceException(e);
		}
		
		recordId = (recordId == null) ? "ignore" : recordId;
			
		String recvfile = getRecordPathFrom(recvfilepath);
		
		InterfaceLog log = new InterfaceLog(recordId, this.interfaceId);
		log.setSendRecord(sendfile);
		log.setSendTime(timeNow);

		log.setRecvRecord(recvfile);
		log.setRecvTime(timeNow);
		
		this.interfaceLogService.insert(log);
		log.setSaved(true);
	}
	
	private String getfileName(String fileName, boolean sendFile) {
		if (StringUtil.hasText(fileName)) {
			return fileName;
		}
		
		return sendFile ? this.streamNameUtil.getSendName() : this.streamNameUtil.getRecvName();
	}
	
	/**
	 * 二进制日志路径
	 * 
	 * @param file
	 * @return
	 */
	private String getRecordPathFrom(String file) {
		if (file == null) {
			return null;
		}
		int splitBegin = file.indexOf(FrameworkConstants.STREAM_LOG_PATH_STOKEN);
		if (splitBegin >= 0) {
			splitBegin = splitBegin + FrameworkConstants.STREAM_LOG_PATH_STOKEN.length() + 1;
		} else {
			splitBegin = 0;
		}
		
		return file.substring(splitBegin);
	}

	public void setStreamNameUtil(StreamNameUtil streamNameUtil) {
		this.streamNameUtil = streamNameUtil;
	}

	public void setStreamOutput(StreamOutput streamOutput) {
		this.streamOutput = streamOutput;
	}

	public void setInterfaceLogService(InterfaceLogService interfaceLogService) {
		this.interfaceLogService = interfaceLogService;
	}

	public void setInterfaceId(String interfaceId) {
		this.interfaceId = interfaceId;
	}

	public void setStreamRecord(boolean streamRecord) {
		this.streamRecord = streamRecord;
	}
}
